package com.ciwong.epaper.modules.epaper.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.EpaperInfo;
import com.ciwong.epaper.modules.msg.bean.WorkContent;
import com.ciwong.epaper.modules.scan.adapter.BaseAdapter;
import com.ciwong.epaper.util.DisplayUtils;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.GlideRoundTransform;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

import java.util.List;

/**
 * @description:搜索配音结果适配器
 * @author: wyk
 * @time: 2022/1/18 12:01
 */
public class SearchDubVideoAdapter extends BaseQuickAdapter<WorkContent, SearchDubVideoAdapter.BookDeskViewHolder> {


    public SearchDubVideoAdapter(int layoutResId, @Nullable List<WorkContent> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(SearchDubVideoAdapter.BookDeskViewHolder helper, WorkContent item) {
        helper.bindData(item);
    }


    public static class BookDeskViewHolder extends BaseViewHolder {
        private Uri uri;
        private WorkContent mWorkContent;
        private SimpleDraweeView mIvBook;
        private TextView mTvBookName;

        public BookDeskViewHolder(View view) {
            super(view);
        }

        public void bindData(WorkContent workContent) {
            mWorkContent = workContent;
            mIvBook = getView(R.id.iv_icon);
            mTvBookName = getView(R.id.tv_name);
            if (workContent != null) {
                if (!TextUtils.isEmpty(workContent.getCover())) {
                    uri = Uri.parse(workContent.getCover());
                    setNetImg(mIvBook, uri, mIvBook.getWidth(), mIvBook.getHeight());
                } else {
                    setLocalDefaultImg(mIvBook);
                }
                mTvBookName.setText(workContent.getResourceName());
            }
        }


        private void setNetImg(SimpleDraweeView simpleDraweeView, Uri uri, int width, int height) {
            try {
                ImageRequest request = null;
                if (uri != null) {
                    request = ImageRequestBuilder.newBuilderWithSource(uri)
                            .setResizeOptions(new ResizeOptions(DisplayUtils.dip2px(simpleDraweeView.getContext(), 100), DisplayUtils.dip2px(simpleDraweeView.getContext(), 200))).build();

                    DraweeController drawweeController = Fresco.newDraweeControllerBuilder().setUri(uri).setImageRequest(request)
                            //列表滑动会变流畅
                            .setOldController(simpleDraweeView.getController())
                            //                    .setAutoPlayAnimations(true)
                            .build();
                    simpleDraweeView.setController(drawweeController);
                }
            } catch (Exception e) {

            } catch (OutOfMemoryError outOfMemoryError) {

            }


        }

        private void setLocalDefaultImg(SimpleDraweeView simpleDraweeView) {
            Uri uri = Uri.parse("res:///" + R.mipmap.item_dub_video_default);
            DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setUri(uri)
                    .setTapToRetryEnabled(true)
                    .build();
            simpleDraweeView.setController(controller);
        }
    }
}
